public class h_4_FizzBuzz {
     volatile int count=1;
    private int n;
    public h_4_FizzBuzz(int n){
        this.n=n;
    }
    public synchronized void fizz() {
        while(!(count%3==0 && count%5!=0) &&count<n){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if(count%3==0 && count%5!=0){
            System.out.print("fizz ");
            count++;
            notifyAll();
        }
    }          // only output "fizz"
    public synchronized void buzz() {
        while(!(count%5==0 && count%3!=0) && count<=n){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if(count%5==0 && count%3!=0){
            System.out.print("buzz ");
            count++;
            notifyAll();
        }
    }          // only output "buzz"
    public synchronized void fizzbuzz() {
        while(!(count%3==0 && count%5==0) && count<=n){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if(count%3==0 && count%5==0 && count<=n){
            System.out.print("fizzbuzz ");
            count++;
            notifyAll();
        }
    }  // only output "fizzbuzz"
    public synchronized void number() {
        while(!(count%3!=0 && count%5!=0) && count<=n){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if(count%3!=0 && count%5!=0){
            System.out.print(count+" ");
            count++;
            notifyAll();
        }
    }      // only output the numbers

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getN() {
        return n;
    }
}
